<template>
	<view class="contribute">
		<u-form :model="form" ref="uForm" labelWidth="200">
			<view class="u-p-x-30">
				<u-form-item label="赠送账号" prop="mobile" :border-bottom="false">
					<u-input v-model="form.mobile" type="number" @input="getNeedCredit"  placeholder="请填写被赠送人的手机号"/>
				</u-form-item>
				<u-form-item label="赠送数量" prop="num" :border-bottom="false">
					<u-input v-model="form.num" type="number" @input="getNeedCredit" placeholder="请填写赠送数量"/>
				</u-form-item>
				<u-form-item label="需要记账点" prop="entryPoint" :border-bottom="false">
					<u-input v-model="form.entryPoint" disabled :placeholder="form.entryPoint || '赠送需要的记账点'"/>
				</u-form-item>
				<u-form-item label="账户余额价值" prop="balance" :border-bottom="false">
					<u-input v-model="form.balance" disabled placeholder=" "/>
				</u-form-item>
			</view>
		</u-form>
			<view class="text-center bg-white u-p-b-30 position-fixed submit">
				<dy-button width="70vw" size="default" @click="submit">确认赠送</dy-button>
			</view>	
	</view>
</template>

<script>
	import { BaseCenterApi, MineCenterApi } from '@/api/serves.js'
	import { mapState,mapActions } from 'vuex'
	export default {
		data() {
			return {
				form:{
					mobile:'',  // 账户
					num:'',    // 数量
					entryPoint: '',  // 记账点
					balance: ''  // 账户余额价值
				},		
			}
		},
		computed:{
			...mapState('user', ['userInfo']),
		},
		mounted() {
			this.showTotalPrice()
		},
		methods: {
			...mapActions({
				getUserInfo: 'user/getUserInfo'
			}),
			// 显示剩余余额价值
			showTotalPrice(){
				this.form.balance = this.userInfo.total_price
			},
			// 获取需要的记账点
			async getNeedCredit(){
				let {mobile,num} = this.form
				if(!num || !mobile) {
					this.form.entryPoint = ''
					return
				}
				let res = await MineCenterApi.getMerch({mobile,num})
				if(res.code != 1){
					this.$Toast(res.msg,1000)
					return
				}
				this.form.entryPoint = res.data.need_credit4
			},
			// 确认赠送
			async submit(){
				let {mobile,num} = this.form
				let params = {
					mobile,num
				}
				if(!mobile) return this.$Toast('请填写需要赠送的账号')
				if(!num) return this.$Toast('请填写需要赠送的数量')
				let res = await MineCenterApi.merchSend(params)
				console.log(res);
				if(res.code == 1){
					this.$Toast('赠送成功')
					//更新账户余额价值
					await this.getUserInfo()
					this.showTotalPrice()
				}else{
					this.$Toast(res.msg)
				}
				
			}
		}
	}
</script>

<style scoped lang="scss">
	.contribute{
		background-color: #fff;
		height: 100vh;
		::v-deep .u-form-item--right{
			border-bottom: 2rpx solid #eee;
			padding: 0 20rpx;
		}
		.submit{
			bottom: 50rpx;
			left: 15vw;
		}
	}
</style>
